package weightUtils.digitalLock;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.example.jhlibrarys.R;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 自定义数字输入框
 * Created by Administrator on 2017/8/2.
 */

public class DigitalInputView extends FrameLayout implements View.OnClickListener {

    private StringBuilder mPassCode = new StringBuilder();
    private int mPasscodeLength = 6;
    private Disposable subscribe;
    int index = 0;
    private OnDigitalChangeListener mListener;

    public DigitalInputView(@NonNull Context context) {
        this(context, null);

    }

    public DigitalInputView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        inflate(getContext(), R.layout.view_digital_lock, this);
        init();
    }

    private void init() {
        TextView number0 = (TextView) findViewById(R.id.number0);
        TextView number1 = (TextView) findViewById(R.id.number1);
        TextView number2 = (TextView) findViewById(R.id.number2);
        TextView number3 = (TextView) findViewById(R.id.number3);
        TextView number4 = (TextView) findViewById(R.id.number4);
        TextView number5 = (TextView) findViewById(R.id.number5);
        TextView number6 = (TextView) findViewById(R.id.number6);
        TextView number7 = (TextView) findViewById(R.id.number7);
        TextView number8 = (TextView) findViewById(R.id.number8);
        TextView number9 = (TextView) findViewById(R.id.number9);
        TextView numberB = (TextView) findViewById(R.id.numberB);

        number0.setOnClickListener(this);
        number1.setOnClickListener(this);
        number2.setOnClickListener(this);
        number3.setOnClickListener(this);
        number4.setOnClickListener(this);
        number5.setOnClickListener(this);
        number6.setOnClickListener(this);
        number7.setOnClickListener(this);
        number8.setOnClickListener(this);
        number9.setOnClickListener(this);

        numberB.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteChar();
            }
        });

        numberB.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                deleteAllCharWithStep();
                return false;
            }
        });

        number0.setTag(0);
        number1.setTag(1);
        number2.setTag(2);
        number3.setTag(3);
        number4.setTag(4);
        number5.setTag(5);
        number6.setTag(6);
        number7.setTag(7);
        number8.setTag(8);
        number9.setTag(9);

    }

    private void deleteAllCharWithStep() {
        final int length = mPassCode.length();
        subscribe = Observable.interval(100, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        if (index >= length) {
                            subscribe.dispose();
                            index = 0;
                            return;
                        }
                        deleteChar();
                        index++;
                    }
                });
    }

    private void deleteChar() {
        if (mPassCode.length() <= 0) {
            return;
        }
        mPassCode.deleteCharAt(mPassCode.length() - 1);
        codeChange();
    }

    @Override
    public void onClick(View view) {
        int number = (int) view.getTag();
        addCode(number);
    }

    private void addCode(int number) {
        if (mPassCode.length() >= mPasscodeLength)
            return;
        mPassCode.append(number);
        codeChange();
    }

    public StringBuilder getPassCode() {
        return mPassCode;
    }

    private void codeChange() {
        if (mListener != null) {
            mListener.digitalChange(getPassCode());
        }
    }

    public void clearPassDigital() {
        mPassCode.delete(0, mPassCode.length());
        codeChange();
    }

    public void setOnDigitalChangeListener(OnDigitalChangeListener mListener) {
        this.mListener = mListener;
    }

    public interface OnDigitalChangeListener {
        void digitalChange(StringBuilder passCode);
    }
}
